package com.apes.fn.scm.merchant.disbursement.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: Disbursement
 * @Description: 赋能商户结算排款单
 * @author: cyy
 * @create: 2023/2/08 11:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Disbursement.CONST_TABLE_NAME)
public class Disbursement extends Model {
    public static final String CONST_TABLE_NAME = "fn_merchant_disbursement";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能商户结算排款单", prefix = "PK%(platformCompany)", padding = 5, useDateRange=true)
    @Comment("编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_company_id", referencedColumnName = "id")
    @Comment("平台公司")
    private PartyRole platformCompany;

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("总排款额")
    private double amount = 0;

    @Column(name = "state")
    @States(name = "状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "apply", name = "申请"),
            @State(id = "approve", name = "审核"),
            @State(id = "delete", name = "删除"),
    })
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveId;

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "apply_id", referencedColumnName = "id")
    @Comment("申请人")
    private PartyRole applyId;

    @Column(name = "apply_date")
    @Comment("申请日期")
    private Date applyDate;

    @Name
    @Column(name = "note", length = 120)
    @Comment("备注")
    private String note;

    @OneToMany(mappedBy = "disbursement", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<DisbursementItem> disbursementItems = new ArrayList();

    public List<DisbursementItem> getDisbursementItems() {
        if(disbursementItems == null) return new ArrayList();
        for (DisbursementItem item : disbursementItems) {
            item.setDisbursement(this);
        }
        return disbursementItems;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
